package com.mayikt.loadbalance;

import org.springframework.cloud.client.ServiceInstance;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @program: mayikt-springcloud-alibaba
 * @description
 * @author: tianya
 * @create: 2020-05-22 16:15
 **/
@Component
public class RotationLoadBalancer  implements LoadBalancer{

    /**
     * 从0开始计数
     */
    private AtomicInteger atomicInteger = new AtomicInteger(0);

    @Override
    public ServiceInstance getSingleAddress(List<ServiceInstance> serviceInstances) {
        int index = atomicInteger.incrementAndGet() % serviceInstances.size();
        return serviceInstances.get(index);
    }
}
